package com.dartsstats.datasource;

import java.util.ArrayList;
import java.util.List;

import com.dartsstats.GameLineup;
import com.dartsstats.DM.GameDM;
import com.dartsstats.DM.GameLineupDM;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class GameLineupDataSource {
	
	private SQLiteDatabase database;
	private GameLineupDM gameLinupDM;
	private String[] allColumns = { GameLineupDM.COLUMN_ID, GameLineupDM.COLUMN_GAME,  GameLineupDM.COLUMN_PLAYER, GameLineupDM.COLUMN_POSITION, GameLineupDM.COLUMN_INNING_ENTERED, GameLineupDM.COLUMN_INNING_LEFT};
	
	public GameLineupDataSource(Context context) {
		gameLinupDM = new GameLineupDM(context);
	}
	
	public void open() throws SQLException {
		database = gameLinupDM.getWritableDatabase();
	}
	
	public void close() {
		gameLinupDM.close();
	}
	
	public GameLineup createGameLineup(long game, long player, int position, int inningEntered, int inningLeft){
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(GameLineupDM.COLUMN_GAME, game);
		values.put(GameLineupDM.COLUMN_PLAYER, player);
		values.put(GameLineupDM.COLUMN_POSITION, position);
		values.put(GameLineupDM.COLUMN_INNING_ENTERED, inningEntered);
		values.put(GameLineupDM.COLUMN_INNING_LEFT, inningLeft);
		long insertId = database.insert(GameLineupDM.TABLE_GAME_LINEUP, null, values);
		Cursor cursor = database.query(GameLineupDM.TABLE_GAME_LINEUP, allColumns, GameLineupDM.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		GameLineup gameLineup = cursorToGameLineup(cursor);
		cursor.close();
		return gameLineup;
	}
	
	public List<GameLineup> getGameLineupByGame(long gameID) {
		openDatabaseIfNull();
		List<GameLineup> fullGameLineup = new ArrayList<GameLineup>();
		String[] whereArgs = new String[] {String.valueOf(gameID)};
		Cursor cursor = database.query(GameLineupDM.TABLE_GAME_LINEUP, allColumns, GameLineupDM.COLUMN_GAME + " = ?", whereArgs, null, null, null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			GameLineup gameLineup = cursorToGameLineup(cursor);
			fullGameLineup.add(gameLineup);
			cursor.moveToNext();
		}
		cursor.close();
		return fullGameLineup;
	}
	
	public void updateGameLineup(GameLineup gameLineup) {
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(GameLineupDM.COLUMN_POSITION, gameLineup.getPosition());
		values.put(GameLineupDM.COLUMN_INNING_ENTERED, gameLineup.getInningEntered());
		values.put(GameLineupDM.COLUMN_INNING_LEFT, gameLineup.getInningLeft());
		String[] whereArgs = new String[] {String.valueOf(gameLineup.getId())};
		database.update(GameDM.TABLE_GAME, values, GameDM.COLUMN_ID + " = ?", whereArgs);
	}
	
	public void deleteGameLineup(GameLineup gameLineup) {
		openDatabaseIfNull();
		long id = gameLineup.getId();
		System.out.println("GameLineup deleted with id: " + id);
		database.delete(GameLineupDM.TABLE_GAME_LINEUP, GameLineupDM.COLUMN_ID + " = " + id, null);
	}
	
	private GameLineup cursorToGameLineup(Cursor cursor) {
		GameLineup gameLineup = new GameLineup();
		gameLineup.setId(cursor.getLong(0));
		gameLineup.setGame(cursor.getInt(1));
		gameLineup.setPlayer(cursor.getInt(2));
		gameLineup.setPosition(cursor.getInt(3));
		gameLineup.setInningEntered(cursor.getInt(4));
		gameLineup.setInningLeft(cursor.getInt(5));
		return gameLineup;
	}
	
	private void openDatabaseIfNull(){
		if(database==null){
			open();
		}
	}

}
